package com.ebupt.migu.music.commons.selfdom.demo.impl.identifier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.globalpopup.enums.FileTypeEnum;
import com.ebupt.migu.globalpopup.enums.IdentifierEnum;
import com.ebupt.migu.music.commons.utils.PathBuffer;
import com.ebupt.migu.music.commons.utils.TxtUtil;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 达人字符类型实现类
 *
 * @author wangzhu
 * @since 2021-12-31
 */
@Slf4j
@Component
public  class AbstractIdentifierExImpl extends AbstractIdentifier {


    /**
     * 等于
     */
    private boolean equalTo(List source, String target  ) {
        return  source.contains(target);
    }

    /**
     * 不等于
     */
    private boolean noEqualTo(List source, String target ) {
        return  !source.contains(target);
    }

    /**
     * 列表等于(文件)
     */
    private boolean listEqualTo(List source,String target ) {
        String file = PathBuffer.NUMBERLOCATION.get("FILE_GLOBAL_PATH") + FileTypeEnum.FILE_TYPE_ENUM_TXT.getFile()+"/"+target;
        TxtUtil txtUtil = TxtUtil.getTxtUtil();
        List targets = txtUtil.toList(new File(file));
        targets.retainAll(source);
        int size = targets.size();
        return size>0? true:false;
    }


    /**
     * 列表不等于(文件)
     */
    private boolean listNoEqualTo(List source,String target) {
        String file = PathBuffer.NUMBERLOCATION.get("FILE_GLOBAL_PATH")+ FileTypeEnum.FILE_TYPE_ENUM_TXT.getFile()+"/"+target;
        TxtUtil txtUtil = TxtUtil.getTxtUtil();
        List targets = txtUtil.toList(new File(file));
        targets.retainAll(source);
        int size = targets.size();
        return size>0? false:true;
    }

    @Override
    public boolean getIdentifier(String source, String target, Integer identifier, Integer conditionKey) {
        return false;
    }

    /**
     * 更具不同的判断方法返回判断结果
     */
    public boolean getIdentifier(List source, String target, Integer identifier,Integer conditionKey) {
        boolean b = true;
        switch (identifier) {
            case 1:
                b = this.equalTo(source, target);
                break;
            case 6:
                b = this.noEqualTo(source, target);
                break;
            case 7:
                b = this.listEqualTo(source, target);
                break;
            case 8:
                b = this.listNoEqualTo(source, target);
                break;
            default:
                break;
        }
        log.info("判断条件:{},对比参数:{},目标参数:{},对比结果:{}", IdentifierEnum.getCodeByName(identifier),source,target,b);
        return b;
    }

    @Override
    public boolean getRelation(LinkedHashMap<Boolean,Integer> map) {
        boolean t = true;
        Iterator<Map.Entry<Boolean, Integer>> iterator = map.entrySet().iterator();
        int  size=0;
        while (iterator.hasNext()) {
            Map.Entry<Boolean, Integer> next = iterator.next();
           if(size==0) {
               t = next.getKey();
           }
                //and
                if (next.getValue() == 1) {
                    t = t && next.getKey();
                }
                //or
                if (next.getValue() == 2) {
                    t = t || next.getKey();
                }
            size+=1;
        }
        return t;
    }

    @Override
    public <T> void sort(List<T> list) {
//        log.info("排序前集合:{},数量:{}",list, list.size() );
        list.sort((x,y)->{
            int compare = 0;
            JSONObject xparse = JSONObject.parseObject(JSON.toJSONString(x));
            JSONObject yparse =JSONObject.parseObject(JSON.toJSONString(y));
            if (xparse.get("conditionKey") != null && yparse.get("conditionKey")!=null) {
                Integer xconditionKey = Integer.valueOf(xparse.get("conditionKey").toString());
                Integer yconditionKey = Integer.valueOf(yparse.get("conditionKey").toString());
                compare = Integer.compare(xconditionKey, yconditionKey);
            }
            return compare;
        });
//        log.info("排序后集合:{},数量:{}",list, list.size() );
    }

}
